package com.cx.controller.zd;

import com.cx.config.RequestResult;
import com.cx.constant.ConstantData;
import com.cx.pojo.Game;
import com.cx.pojo.ShoppingCar;
import com.cx.pojo.User;
import com.cx.service.zd.TrolleyService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("shoppingcar")
public class TrolleyController {
    @Autowired
    TrolleyService trolleyService;

    @RequestMapping("addcar")
    @ResponseBody
    public String addCar(HttpSession session, Integer gameId){
        User user = (User) session.getAttribute(ConstantData.SESSION_KEY);
        int userId = user.getUserId();
        int affect = trolleyService.addCar(userId,gameId);
        if(affect>0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping("showcar")
    @ResponseBody
    public List<ShoppingCar> selectAll(HttpSession session){
        User user = (User) session.getAttribute(ConstantData.SESSION_KEY);
        int userId = user.getUserId();
        System.out.println("userid"+userId);
        List<ShoppingCar> shoppingCarList = trolleyService.selectAll(userId);
        System.out.println();
        return shoppingCarList ;

    }

    @RequestMapping("deletecar")
    @ResponseBody
    public String delete(Integer userId,Integer gameId){
        System.out.println(gameId);
        int affect = trolleyService.delete(userId,gameId);
        if(affect>0)
            return "success";
        else
            return "fail";

    }

    @RequestMapping("deleteall")
    @ResponseBody
    public String deleteAll(Integer userId){
        int affect = trolleyService.deleteAll(userId);
        if(affect>0)
            return "success";
        else
            return "fail";

    }
}
